package com.chansos.dubbogo.hessian_generator;

import com.chansos.dubbogo.common.Constant;

import java.io.BufferedReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class FileReader {
    public static FileInfo read(String path, int defaultLine) {
        BufferedReader buffer = null;
        FileInfo goFile = null;
        try {
            buffer = new BufferedReader(new java.io.FileReader(path));

            goFile = new FileInfo(path);
            StringBuilder stringBuffer = goFile.getStringBuffer();

            int bufferSize;
            int lineSize;
            int lineNumber = 0;

            String line;
            Matcher matcher;
            while ((line = buffer.readLine()) != null) {
                bufferSize = stringBuffer.length();
                lineSize = line.length();

                if (goFile.isHasInitFunction() && lineSize > 0 && line.charAt(0) == Constant.funcEnd) {
                    goFile.setInitFuncEndIndex(bufferSize + 1); // 检测初始化函数结束位
                }

                stringBuffer.append(line).append(Constant.newLine);

                if (lineNumber == defaultLine && Pattern.compile(Constant.GoStructRegexp).matcher(line).find()) { // 检索是否为go struct
                    matcher = Pattern.compile(Constant.GoStructNameRegexp).matcher(line);
                    if (matcher.find()) {
                        goFile.setDefaultStructName(matcher.group().trim());
                    }
                }

                lineNumber++;

                matcher = Pattern.compile(Constant.PackageRegexp).matcher(line);
                if (matcher.find()) { // 检测package位置
                    goFile.setPackageStartIndex(bufferSize);
                    goFile.setPackageEndIndex(bufferSize + lineSize + 1);
                }

                matcher = Pattern.compile(Constant.InitFunctionRegexp).matcher(line);
                if (matcher.find()) { // 检测初始化函数
                    goFile.setHasInitFunction(true);
                    goFile.setInitFuncStartIndex(bufferSize); // 检测初始化函数初始位
                    goFile.setInitFuncStatementStartIndex(bufferSize + lineSize); // 初始化函数方法体初始位
                    continue;
                }

                if (!goFile.isHasHessianImport()) {
                    matcher = Pattern.compile(Constant.HessianImportRegexp).matcher(line);
                    if (matcher.find()) {  // 检测是否已导入hessian2包
                        matcher = Pattern.compile(Constant.LineCommentRegexp).matcher(line.substring(0, line.indexOf(Constant.HessianImportRegexp))); // 是否被行注释
                        goFile.setHasHessianImport(!matcher.find());
                        continue;
                    }
                }

                matcher = Pattern.compile(Constant.HessianPOJORegisterRegexp).matcher(line);
                if (matcher.find()) {  // 校验是否已实现Hessian.POJO
                    matcher = Pattern.compile(Constant.HessianPOJONameRegexp).matcher(matcher.group());
                    if (matcher.find()) { // 检出Go Struct名字
                        String value = matcher.group();
                        goFile.getGoStruct(value.substring(1, value.length() - 1)).setHasHessianPOJOImplements(true);
                        continue;
                    }
                }

                if (Pattern.compile(Constant.GoStructRegexp).matcher(line).find()) { // 检出Go Struct名字
                    matcher = Pattern.compile(Constant.GoStructNameRegexp).matcher(line);
                    if (matcher.find()) {
                        goFile.getGoStruct(matcher.group().trim());
                    }
                }
            }
        } catch (Exception ignore) {
        } finally {
            try {
                assert buffer != null;
                buffer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return goFile;
    }
}
